package ferp.core.ai.tree2;

/**
* User: igorgok
* Date: 7/26/12 11:35 AM
*/
abstract class Pool<T extends LinkedList.Entry<T>>
{
  // preallocate objects inside the pool
  public final void preallocate(long n)
  {
    this.objects = initialize();

    created = (int)n;

    while (n-- > 0)
      put(create());
  }

  // get object from pool
  public final T get()
  {
    if (objects.size() > 0)
      return objects.removeFirst();

    ++created;

    return create();
  }

  // put object back to the pool
  public final void put(T object)
  {
    objects.addFirst(object);
  }

  public final int total() {return created;}
  public final int in() {return objects.size();}
  public final int out() {return created - objects.size();}

  protected abstract LinkedList<T> initialize();
  protected abstract T create();

  private LinkedList<T> objects;
  private int created = 0;
}
